package com.hackafone.tale.service.rest.impl;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;
import com.hackafone.tale.service.dao.TailDao;
import com.hackafone.tale.service.dao.TaleDao;
import com.hackafone.tale.service.model.Tail;
import com.hackafone.tale.service.model.Tale;

@Path("/tail/")
public class TailResource
{

	@GET
	@Produces({ "application/json" })
	@Path("{tailId}")
	public Tail getTail(@PathParam("tailId") long tailId)
	{
		return TailDao.INSTANCE.getTailById(tailId);
	}

	@POST
	@Produces(MediaType.TEXT_PLAIN)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String newTaile(
			@FormParam("json") String json,
			@Context HttpServletResponse servletResponse
			) throws IOException
	{
		Gson gson = new Gson();

		Tail tail = gson.fromJson(json, Tail.class);
		TailDao.INSTANCE.add(tail);

		return Long.toString(tail.getId());
	}

}
